package com.tao.aszx.manager.controller;


import com.atguigu.spzx.utils.AuthContextUtil;
import com.tao.aszx.manager.service.SysMenuService;
import com.tao.aszx.manager.service.SysUserService;
import com.tao.aszx.manager.service.ValidateCodeService;
import com.tao.aszx.model.dto.system.LoginDto;
import com.tao.aszx.model.entity.system.SysUser;
import com.tao.aszx.model.vo.system.LoginVo;
import com.tao.aszx.model.vo.common.Result;
import com.tao.aszx.model.vo.common.ResultCodeEnum;
import com.tao.aszx.model.vo.system.SysMenuVo;
import com.tao.aszx.model.vo.system.ValidateCodeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 主页
 */

@Tag(name="首页")
@RestController
@RequestMapping("/admin/system/index")
//@CrossOrigin(allowCredentials = "true" , originPatterns = "*" , allowedHeaders = "*")
public class IndexController {
    @Autowired
    SysUserService sysUserService;

    @Autowired
    SysMenuService sysMenuService;

    @Autowired
    private ValidateCodeService validateCodeService;

    @Operation(summary = "登录接口")
    @PostMapping(value = "/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        // 登录成功后，返回token
        LoginVo loginVo = sysUserService.selectSysUserByUser(loginDto);


        return Result.build(loginVo, ResultCodeEnum.SUCCESS) ;
    }


    @Operation(summary = "验证码接口")
    @GetMapping(value = "/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();

        return Result.build(validateCodeVo , ResultCodeEnum.SUCCESS) ;
    }

    /**
     * 退出
     * @param token
     * @return
     */
    @Operation(summary = "用户退出")
    @GetMapping(value = "/logout")
    public Result logout(@RequestHeader(value = "token") String token) {
        sysUserService.deleteToken(token);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }


    @Operation(summary = "获取用户接口")
    @GetMapping(value = "/userinfo")
    public Result<SysUser> getUserInfo(@RequestHeader(name="token") String token){
//        SysUser sysUser = sysUserService.getUserInfo(token);
        SysUser sysUser = AuthContextUtil.get();
        return Result.build(sysUser , ResultCodeEnum.SUCCESS) ;
    }

    @GetMapping("/menus")
    public Result menus() {
        List<SysMenuVo> sysMenuVoList =  sysMenuService.findUserMenuList() ;
        return Result.build(sysMenuVoList , ResultCodeEnum.SUCCESS) ;
    }

}
